<template>
  <div class="register-container">
    <el-card class="register-card">
      <h1 class="register-title">网上约课系统注册</h1>
      <div class="register-form">
        <el-form :model="registerForm" :rules="rules" ref="registerFormRef" @submit.prevent="handleRegister">
          <el-row>
            <el-col :span="24">
              <el-form-item label="用户名" label-width="70px" prop="name">
                <el-input v-model="registerForm.name" autocomplete="off" maxlength="10" show-word-limit></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="手机号" label-width="70px" prop="phone">
                <el-input v-model="registerForm.phone" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="16">
              <el-form-item label="密码" label-width="70px" prop="password">
                <el-input type="password" v-model="registerForm.password" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="性别" label-width="70px" prop="sex">
                <el-radio-group v-model="registerForm.sex">
                  <el-radio :label="'男'">男</el-radio>
                  <el-radio :label="'女'">女</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item>
                <el-button type="primary" @click="handleRegister">注册</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <!-- 已有帐号，点此登录 -->
      <el-row style="text-align: right;">
        <el-col>
          <el-text class="w-150px mb-2">
            已有帐号，点此>> <RouterLink to="/login" style="font-size: 1.2em;">登录</RouterLink>
          </el-text>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { register } from '@/api/register';

onMounted(() => {
  handleRegister();
});

const registerFormRef = ref(null);

const registerForm = ref({
  name: '',
  phone: '',
  password: '',
  sex: '男', // 默认性别为男
});

const rules = ref({
  name: [
    { required: true, message: '用户名是必填项', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
  ],
  phone: [
    { required: true, message: '手机号是必填项', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码是必填项', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ],
  sex: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ]
});

function handleRegister() {
  registerFormRef.value.validate((valid) => {
    if (valid) {
      console.log('注册信息:', registerForm.value);
      register(registerForm.value)
    } else {
      console.error('表单验证失败');
      return false;
    }
  });
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-image: url('@/assets/imgs/2.jpg');
  background-size: cover;
  background-position: center;
}

.register-card {
  width: 420px;
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8); /* 半透明白色背景 */
  border-radius: 8px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.register-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.register-form {
  width: 100%;
}
</style>
